home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / windows / doc / ui / overview / example / converter.000 < prev    next >
Encoding:
Text File  |  1996-02-26  |  7.0 KB  |  263 lines

  1. <!--NewPage-->
  2. <html>
  3. <head>
  4. <title>Converter.java</title>
  5. </head>
  6. <body>
  7. <p>
  8. <hr size=4>
  9.  
  10. <h2>
  11.     Converter.java
  12. </h2>
  13. <p>
  14. <blockquote>
  15.  
  16. <pre>
  17.  
  18. /*
  19.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  20.  *
  21.  * Permission to use, copy, modify, and distribute this software
  22.  * and its documentation for NON-COMMERCIAL purposes and without
  23.  * fee is hereby granted provided that this copyright notice
  24.  * appears in all copies. Please refer to the file "copyright.html"
  25.  * for further important copyright and licensing information.
  26.  *
  27.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  28.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  29.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  30.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  31.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  32.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  33.  */
  34. /* This program could use some layout work, and the functionality
  35.  * could use some tweaking, but it seems to basically work.
  36.  */
  37. import java.awt.*;
  38. import java.util.*;
  39. import java.applet.Applet;
  40.  
  41. public class Converter extends Applet {
  42.     Frame window;
  43.     ConversionPanel metricPanel, usaPanel;
  44.     Unit metricDistances[] = new Unit[3];
  45.     Unit usaDistances[] = new Unit[4];
  46.  
  47.     /** Create the ConversionPanels (one for metric, another for U.S.).
  48.       * I used "U.S." because although Imperial and U.S. distance
  49.       * measurements are the same, this program could be extended to
  50.       * include volume measurements, which aren't the same.
  51.       */
  52.     public void init() {
  53.     setLayout(new GridLayout(2,0,5,5));
  54.  
  55.     metricDistances[0] = new Unit("Centimeters", 0.01);
  56.     metricDistances[1] = new Unit("Meters", 1.0);
  57.     metricDistances[2] = new Unit("Kilometers", 1000.0);
  58.     metricPanel = new ConversionPanel(this, "Metric System",
  59.                         metricDistances);
  60.  
  61.     usaDistances[0] = new Unit("Inches", 0.0254);
  62.     usaDistances[1] = new Unit("Feet", 0.305);
  63.     usaDistances[2] = new Unit("Yards", 0.914);
  64.     usaDistances[3] = new Unit("Miles", 1613.0);
  65.     usaPanel = new ConversionPanel(this, "U.S. System", usaDistances);
  66.  
  67.     add(metricPanel);
  68.     add(usaPanel);
  69.     }
  70.  
  71.     /** Does the conversion from metric to U.S., or vice versa, and
  72.       * updates the appropriate ConversionPanel. */
  73.     void convert(ConversionPanel from) {
  74.     ConversionPanel to;
  75.  
  76.     if (from == metricPanel)
  77.         to = usaPanel;
  78.     else
  79.         to = metricPanel;
  80.     double multiplier = from.getMultiplier() / to.getMultiplier();
  81.     to.setValue(from.getValue() * multiplier);
  82.     }
  83.  
  84.     /** Draws a box around this panel. */
  85.     public void paint(Graphics g) {
  86.     Dimension d = size();
  87.     g.drawRect(0,0, d.width - 1, d.height - 1);
  88.     }
  89.     
  90.     /** Puts a little breathing space between
  91.       * the panel and its contents, which lets us draw a box
  92.       * in the paint() method.
  93.       */
  94.     public Insets insets() {
  95.     return new Insets(5,5,5,5);
  96.     }
  97.  
  98.     public static void main(String args[]) {
  99.     Frame f = new Frame("Converter Applet/Application");
  100.     Converter converter = new Converter();
  101.  
  102.     converter.init();
  103.  
  104.     f.add("Center", converter);
  105.     f.pack();
  106.     f.show();
  107.     }
  108.  
  109. }
  110.  
  111.  
  112. class ConversionPanel extends Panel {
  113.     String title;
  114.     TextField textField;
  115.     Scrollbar slider;
  116.     Choice unitChooser;
  117.     int min = 0;
  118.     int max = 10000;
  119.     Converter controller;
  120.     Unit units[];
  121.  
  122.     //TO DO: Should make both panels' choices the same width.
  123.     ConversionPanel(Converter myController, String myTitle, Unit myUnits[]) {
  124.     super();
  125.     GridBagConstraints c = new GridBagConstraints();
  126.     GridBagLayout gridbag = new GridBagLayout();
  127.     setLayout(gridbag);
  128.     controller = myController;
  129.     title = myTitle;
  130.     units = myUnits;
  131.  
  132.     //Set up default constraints
  133.     c.fill = GridBagConstraints.HORIZONTAL;
  134.  
  135.     //Add the label
  136.     Label label = new Label(title, Label.CENTER);
  137.     c.weightx = 0.0;
  138.     c.gridwidth = GridBagConstraints.REMAINDER;
  139.     gridbag.setConstraints(label, c);
  140.     add(label);
  141.  
  142.     //Add the text field
  143.     textField = new TextField("0", 10);
  144.     c.weightx = 1.0;
  145.     c.gridwidth = GridBagConstraints.RELATIVE;
  146.     gridbag.setConstraints(textField, c);
  147.     add(textField);
  148.  
  149.     //Add the pop-up list (Choice)
  150.     unitChooser = new Choice();
  151.     for (int i = 0; i < units.length; i++) {
  152.         unitChooser.addItem(units[i].description);
  153.     }
  154.     c.weightx = 0.0;
  155.     c.gridwidth = GridBagConstraints.REMAINDER;
  156.     gridbag.setConstraints(unitChooser, c);
  157.     add(unitChooser);
  158.  
  159.     //Add the slider
  160.     slider = new Scrollbar(Scrollbar.HORIZONTAL, 0, 100, min, max);
  161.     c.weightx = 0.0;
  162.     c.gridheight = 1;
  163.     c.gridwidth = GridBagConstraints.RELATIVE;
  164.     gridbag.setConstraints(slider, c);
  165.     add(slider);
  166.     }
  167.  
  168.     /** Returns the multiplier (units/meter) for the currently
  169.       * selected unit of measurement.
  170.       */
  171.     double getMultiplier() {
  172.     int i = unitChooser.getSelectedIndex();
  173.     return (units[i].multiplier);
  174.     }
  175.  
  176.     /** Draws a box around this panel. */
  177.     public void paint(Graphics g) {
  178.     Dimension d = size();
  179.     g.drawRect(0,0, d.width - 1, d.height - 1);
  180.     }
  181.     
  182.     /** Puts a little breathing space between
  183.       * the panel and its contents, which lets us draw a box
  184.       * in the paint() method.
  185.       * We add more pixels to the right, to work around a
  186.       * Choice bug.
  187.       */
  188.     public Insets insets() {
  189.     return new Insets(5,5,5,8);
  190.     }
  191.  
  192.     /** Gets the current value in the text field.
  193.       * That's guaranteed to be the same as the value
  194.       * in the scroller (subject to rounding, of course).
  195.       */
  196.     double getValue() {
  197.     double f;
  198.     try {
  199.         f = Double.valueOf(textField.getText()).doubleValue(); 
  200.     } catch (java.lang.NumberFormatException e) {
  201.         f = 0.0;
  202.     }
  203.     return f;
  204.     }
  205.  
  206.     /** Respond to user actions. */
  207.     public boolean handleEvent(Event e) {
  208.     if (e.target instanceof Scrollbar) {
  209.         textField.setText(String.valueOf(slider.getValue()));
  210.         controller.convert(this);
  211.     } else if ((e.target instanceof TextField) 
  212.            && (e.id == Event.ACTION_EVENT)) {
  213.         setSliderValue(getValue());
  214.         controller.convert(this);
  215.     } else if ((e.target instanceof Choice) 
  216.            && (e.id == Event.ACTION_EVENT)) {
  217.         controller.convert(this);
  218.     } 
  219.     return false;
  220.     }
  221.  
  222.     /** Set the values in the slider and text field. */
  223.     void setValue(double f) {
  224.     setSliderValue(f);
  225.         textField.setText(String.valueOf(f));
  226.     }
  227.  
  228.     /** Set the slider value. */
  229.     void setSliderValue(double f) {
  230.     int sliderValue = (int)f;
  231.  
  232.     if (sliderValue > max)
  233.            sliderValue = max;
  234.     if (sliderValue < min)
  235.         sliderValue = min;
  236.         slider.setValue(sliderValue);
  237.     }
  238. }
  239.  
  240.  
  241. class Unit {
  242.     String description;
  243.     double multiplier;
  244.  
  245.     Unit(String description, double multiplier) {
  246.     super();
  247.     this.description = description;
  248.     this.multiplier = multiplier;
  249.     }
  250.  
  251.     public String toString() {
  252.     String s = "Meters/" + description + " = " + multiplier;
  253.     return s;
  254.     }
  255. }
  256. </pre>
  257. </blockquote>
  258. <p>
  259. <hr size=4>
  260. <p>
  261. </body>
  262. </html>
  263.